﻿using Mapster;
using SqlSugar;
using Zhp.Common.WebApp;
using Zhp.Entity.Sys;
using Zhp.IService.Sys;
using Zhp.Model.Sys;
using Zhp.Service.Base;
using Zhp.SqlSugar.Repository;

namespace Zhp.Service.Sys
{
    public class SysUserRoleService : BaseService<SysUserRole>, ISysUserRoleService
    {
        IBaseRepository<SysUser> _sysUserRep;
        IBaseRepository<SysRole> _sysRoleRep;
        public SysUserRoleService(ICurrentUser currentUser, IBaseRepository<SysUserRole> rep, IBaseRepository<SysUser> sysUserRep, IBaseRepository<SysRole> sysRoleRep)
        {
            _currentUser = currentUser;
            _rep = rep;
            _sysUserRep = sysUserRep;
            _sysRoleRep = sysRoleRep;
        }

        public async Task<(List<UserRoleModel>, List<string>)> GetUserRole(UserRoleQuery query)
        {
            var userList = await _sysUserRep.GetPageListAsync(new List<IConditionalModel>
            {
            }, query, x => x.CreateTime, OrderByType.Desc);

            var userRoleList = await _rep.GetListAsync(x => x.RoleId.Equals(query.RoleId));

            var rList = userList.Adapt<List<UserRoleModel>>();

            foreach (var item in rList)
            {
                item.IsCurrentRole = userRoleList.Exists(x => x.UserId.Equals(item.Id));
            }

            return (rList, userRoleList.Select(x => x.UserId.ToString()).ToList());
        }

        public async Task<bool> Create(UserRoleCreate create)
        {
            using (var uow = _rep.Context.CreateContext(_rep.Context.Ado.IsNoTran()))
            {
                await _rep.DeleteAsync(x => x.RoleId.Equals(create.RoleId));

                var userRoleList = new List<SysUserRole>();
                foreach (var item in create.UserIdList)
                {
                    userRoleList.Add(new SysUserRole
                    {
                        RoleId = create.RoleId,
                        UserId = item,
                    });
                }
                await _rep.InsertRangeAsync(userRoleList);

                return uow.Commit();
            }
        }

        public async Task<(List<UserRoleByUserModel>, List<string>)> GetRoleUserByUser(UserIdQuery query)
        {
            var roleList = await _sysRoleRep.GetPageListAsync(new List<IConditionalModel>
            {
            }, query, x => x.CreateTime, OrderByType.Desc);

            var userRoleList = await _rep.GetListAsync(x => x.UserId.Equals(query.UserId));

            var rList = roleList.Adapt<List<UserRoleByUserModel>>();

            foreach (var item in rList)
            {
                item.IsCurrentUser = userRoleList.Exists(x => x.RoleId.Equals(item.Id));
            }

            return (rList, userRoleList.Select(x => x.RoleId.ToString()).ToList());
        }

        public async Task<bool> CreateByUser(UserRoleByUserCreate create)
        {
            using (var uow = _rep.Context.CreateContext(_rep.Context.Ado.IsNoTran()))
            {
                await _rep.DeleteAsync(x => x.UserId.Equals(create.UserId));

                var userRoleList = new List<SysUserRole>();
                foreach (var item in create.RoleIdList)
                {
                    userRoleList.Add(new SysUserRole
                    {
                        UserId = create.UserId,
                        RoleId = item,
                    });
                }
                await _rep.InsertRangeAsync(userRoleList);

                return uow.Commit();
            }
        }

        public async Task<List<long>> GetRoleIdByCurrentUser(long userId)
        {
            return await _rep.AsQueryable().Where(x => x.UserId == userId).Select(x => x.RoleId).ToListAsync();
        }
    }
}
